package PFSE

import (
	"encoding/json"
	"sort"

	// PF "smartconn.cc/LuoYang/PageFlip"
	"smartconn.cc/tosone/ra_main/common"
	// SE "smartconn.cc/LuoYang/SearchEngine"
)

var err error

// AddFeature 添加页面到 searchengine
func AddFeature(feature common.FeatureWithID) {

}

// RemoveFeature 移除页面从 searchengine
func RemoveFeature(id string) {

}

// RemoveAllFeatures 移除所有的页面从 searchengine
func RemoveAllFeatures() {

}

// Compare compare
func Compare(feature1, feature2 string) int {
	// return PF.CompareFeature(feature1, feature2)
	return 0
}

// SEResult Search engine result
type SEResult struct {
	ID    string  `json:"id"`
	Ratio float64 `json:"ratio"`
}

type sortStructByFloat []SEResult

func (a sortStructByFloat) Len() int           { return len(a) }
func (a sortStructByFloat) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a sortStructByFloat) Less(i, j int) bool { return a[i].Ratio > a[j].Ratio }
func sortSEResult(strs []SEResult) []SEResult {
	sort.Sort(sortStructByFloat(strs))
	return strs
}

// Search search
func Search(feature string) (id string) {
	var res = "[{\"id\":\"123\",\"ratio\":12.10},{\"id\":\"456\",\"ratio\":1.10}]"
	var result []SEResult
	err = json.Unmarshal([]byte(res), &result)
	result = sortSEResult(result)
	// if len(result) != 0 {
	// 	return result[0].ID
	// }
	// return result[0].ID
	return ""
}
